<link href="<?php echo TEMPLATES_PATH;?>/css/home_cart.css" rel="stylesheet" type="text/css">
<header class="layout head padding-small">
    <a href="javascript:pageBack();" class="hBack float-left"></a>
    <h4 class="hTxt float-left text-center text-white">订单支付</h4>
    <a href="javascript:;" id="btnJdkey" class="hHlep float-right"></a>
</header>
<div class="pay_main">
	<!--课程购买-->
<?php if(!empty($output['study_id'])){
	  $url="index.php?act=payment&study_id=".$output['study_id'];
} else { ?>
	<?php
	if(count($output['order']) == 1){
		 //广东省地区内才显示货到付款
		 $areaData=area::allAreaData();
		 $city_id = $areaData[$output['order'][0]['area_id']]['area_parent_id'];
		 $provice_id = $areaData[$city_id]['area_parent_id'];
		 if($provice_id == 19 && $output['order'][0]['store_id'] == OFFICIAL_STORE_ID){
			$is_cod = true;
		 }else if($city_id == 287 && $output['order'][0]['store_id'] == JKY_STORE_ID){
			$is_cod = true;
		 }else{
			$is_cod = false;
		 }
	 }
    $url="index.php?act=payment";
    $rl="";
    foreach ($output['order'] as $key => $order){
          $allprice+=$order['order_amount'];
          $rl.=("&order_id[]=".$order['order_id']);
    };
    $url.=$rl;
    ?>
        <?php }?>
	<form action="<?php echo $url; ?>" method="POST" id="goto_pay">
	<div class="all_order_price">
    	所有订单总金额：<span class="order_price">&yen; <?php echo number_format($output['allamount'],2); ?></span>
    	<p>若有价格修改，请 <a href="javascript:void(0);" id="refresh" onClick="window.location.reload();">刷新>></a></p>
    </div>
    <?php if(!empty($output['study_id'])){
	  $url="index.php?act=payment&study_id=".$output['study_id'];
} else { ?>
    <div class="order_list">
    	<?php foreach($output['order'] as $key=>$row){?>
    	<dl>
        	<dd> 订 单  号 ：<?php echo $row['order_sn']; ?><span class="order_price">&yen; <?php echo number_format($row['order_amount'],2); ?></span></dd>
            <dd>商城名称：<?php echo htmlspecialchars($row['store_name']); ?></dd>
            <dd>店主名称：<?php echo $row['seller_name']; ?></dd>
            <dd>下单时间：<?php echo date('Y-m-d:H:i:s',$row['add_time']); ?></dd>
        </dl>
        <?php } ?>
    </div>
            <?php }?>
    
    <div class="sel_pay_tit">
    	<div class="bor_tit">选择支付方式</div>
    </div>
    
      <div class="tabs-panel">
      
        <?php if($output['is_default_pay'] && $output['available_predeposit'] > 0){?>
        <div class="cart_usebalance">
            <p>预存款可使用余额：<b><?php echo $output['available_predeposit'];?></b>元</p>
            <table>
                <tr>
                    <td>
                        <p><input type="checkbox" name="is_balancepay" value="1" id="check_balancepay"/></p>
                    </td>
                    <td>
                        <p>
                            <label for="check_balancepay">
                            <?php if($output['available_predeposit'] >= $output['allamount']) { $full_banlancepay_pay = 1;?>
                                                                     使用余额付款<b class="balance_block"><?php echo number_format($output['allamount'],2);?></b>元
                            <?php }else{ $full_banlancepay_pay = 0;?>                                            
                                                                     使用余额付款<b class="balance_block"><?php echo $output['available_predeposit'];?></b>元，
                                                                     剩余<b><?php echo number_format($output['allamount'] - $output['available_predeposit'],2);?></b>元可以选择其他付款方式付款。
                            <?php }?>
                            </label>
                        </p>
                    <td>
                </tr>                                            
            </table>
        </div>
       <?php }?>
      
        <ul class="cart-defray">
				 
		 <?php if(!empty($output['offine_array']) && (count($output['order']) == 1) && in_array($output['order'][0]['store_id'],array(OFFICIAL_STORE_ID,JKY_STORE_ID))  && $is_cod ){ ?>
          <?php foreach($output['offine_array'] as $val) { ?>
          <li>
            <label class="radio">
              	<input id="payment_<?php echo $val['payment_code']; ?>" class="radio_pay" type="radio" name="payment_id" value="<?php echo $val['payment_id']; ?>" extendattr="<?php echo $val['payment_code']; ?>" <?php echo $val['payment_code'] == 'predeposit' ? 'checked' : ''?>/>
            	<span class="logo">
                	<img src="<?php echo SiteUrl?>/api/gold_payment/<?php echo $val['payment_code']; ?>/logo.gif" alt="<?php echo $val['payment_name']; ?>" title="<?php echo $val['payment_name']; ?>" />
                </span>
            </label>
          </li>
          <?php } ?>
          <?php } ?>
				 
          <?php if(!empty($output['online_array'])) {
              //手机版的三个支付方式排序下 1支付宝2银联3微信4预存款
              $output['online_arr']=array(5);
          foreach($output['online_array'] as $v) {
              switch($v['payment_code']){
                  case 'alipaywap' : $output['online_arr'][0]=$v;break;
                  case 'chinapaywap' : $output['online_arr'][1]=$v;break;
                  case 'wxpay' : $output['online_arr'][2]=$v;break;
                  case 'predeposit' : $output['online_arr'][3]=$v;break;
				  case 'tenpay' : $output['online_arr'][4]=$v;break;
              }
          }
              ksort($output['online_arr']);
              ?>
          <?php foreach($output['online_arr'] as $val) { ?>
          <?php 
		  	if($val['payment_code'] == 'wxpay'){
			 	$user_agent = $_SERVER['HTTP_USER_AGENT'];
				if (strpos($user_agent, 'MicroMessenger') === false) {
					continue;	//非微信内置浏览器不显示
				} else {
					// 获取版本号
					preg_match('/.*?(MicroMessenger\/([0-9.]+))\s*/', $user_agent, $matches);
					if($matches[2] < 5.0){
						continue;	//微信版本小于5.0不显示
					}
				}
			}
		 ?>
          <li>
            <label class="radio">
              	<input id="payment_<?php echo $val['payment_code']; ?>" class="radio_pay" type="radio" name="payment_id" value="<?php echo $val['payment_id']; ?>" extendattr="<?php echo $val['payment_code']; ?>"
                    <?php echo $val['payment_code'] == 'alipaywap' ? 'checked' : ''?>/>
            	<span class="logo">
                	<img src="<?php echo SiteUrl?>/api/gold_payment/<?php echo $val['payment_code']; ?>/logo.gif" alt="<?php echo $val['payment_name']; ?>" title="<?php echo $val['payment_name']; ?>" />
                </span>
            </label>
          </li>
          <?php } ?>
          <?php } ?>
          
          <?php if($output['is_alliance_pay']){?>
          <input name="payment_id" value="5" type="hidden" />
          <input name="alliance_pay" value="1" type="hidden" />
          <p>品牌预存款可以使用余额：<b><?php echo $output['available_alliance_deposit'];?></b>元</p>
          <p>
              <label for="use_alliance_bond">
              <input id="use_alliance_bond" name="use_alliance_bond" value="1" style="margin-top: 5px;" type="checkbox" <?php if($output['available_alliance_deposit'] >= $output['allamount']){echo ' disabled="true" ';}?>/>
                                可使用保证金：<b><?php echo $output['available_alliance_bond'];?></b>元
              </label>
          </p>
          
          <p id="alliance_pay_tip" style="margin-top: 10px;">
          <?php if($output['available_alliance_deposit'] >= $output['allamount']){?>
                            使用品牌预存款支付<span style="color: #ED8600;padding:0 3px"><?php echo number_format($output['allamount'], 2);?></span>元   
          <?php }else{?>
           <a style="color:red" href="index.php?act=brand_alliance&op=brand_pay&brand_id=<?php echo $output['order'][0]['brand_id'];?>">品牌预存款不足，请点击这里进行充值</a>
          <?php }?>                     
          </p>
          <?php }?>
        </ul>
      </div>
    
    <div class="pay_btn_area"><a href="javascript:void(0);" <?php if($output['is_alliance_pay']){echo ' onclick="show_window(\'ent_pay_pwd\');" ';}else{?>onclick="check();"<?php }?> class="pay_btn">确认支付</a></div>
    
	</form>
</div>

<div id="ent_pay_pwd" class="ent_pay_pwd">
      <div class="close_ent">验证支付密码<a href="javascript:void(0);" onclick="close_ent();"></a></div>
      <div class="ent_pay_main">
      	<input type="password" class="text" autocomplete="off" name="payword" id="payword" value="" placeholder="请输入交易支付密码">
        <span id="error_pwd"></span>
        <div class="enter">
          <a href="javascript:void(0);" class="link" onclick="act()">确认</a>
          <a href="javascript:void(0);" class="link" onclick="window.open('index.php?act=member_index&op=mod_pay_pwd')">设置支付密码</a>
          <a href="javascript:void(0);" class="link" onclick="resetPwd()" style="display:none;" id="find_pay_pwd">找回支付密码</a>
         </div>
      </div>
</div>

<script type="text/javascript">
function act(){
	var pas = $('#payword').val();
	$.post('index.php?act=member_index&op=checkPayPwd',{password:pas},function(date){
		if(date=="true"){
		  $('#goto_pay').submit();
		}else{
		  $("#error_pwd").text('支付密码验证错误');
		  $('#find_pay_pwd').show();  
		}
	},'text');
}

function resetPwd(){
	$.ajax({
		type:'POST',
		url:'index.php?act=member_index&op=resetPayPwd',
		error:function(html){
			alert(html.msg);
		},
		success:function(html){
			alert(html.msg);
		},
		dataType:'json'
	});
}

//支付检测
function check(){
    <?php if($full_banlancepay_pay){?>
    if($('#check_balancepay').is(':checked')){
        show_window('ent_pay_pwd');
        return false;
    }
    <?php }?>

	var $payment_checked = $("input[name='payment_id']:checked");

	if($payment_checked.val() > 0){
		$('#goto_pay').submit();
	}else{
	    alert('请选择支付方式');
	}
}

function close_ent(){
	$('#ent_pay_pwd').hide();
	$('#area_back').hide();
}

//显示遮罩层
function show_window(obj){
	$('#'+obj).show();
	$(window).bind('resize scroll',function(e){
		$('#'+obj).css({
			left: ($(window).width() - $('#'+obj).outerWidth())/2+ $(document).scrollLeft(),
			top: ($(window).height() - $('#'+obj).outerHeight())/2+ $(document).scrollTop()
		});
	});
	$('#'+obj).css({
		left: ($(window).width() - $('#'+obj).outerWidth())/2+ $(document).scrollLeft(),
		top: ($(window).height() - $('#'+obj).outerHeight())/2+ $(document).scrollTop()
	});
	
	if($('#area_back').length<=0){
		$('body').append($('<div>')
			.attr('id','area_back')
			.css({
				position:'absolute',
				'z-index':'9999',
				top:0,
				left:0,
				margin:0,
				background:'#666'
		}));
		$('#area_back').css({
			width:$(document).width(),
			height:$(document).height(),
			opacity:'0.8'
		});
	}
	$('#area_back').show();
}


$(function(){
	//使用保证金
	$("#use_alliance_bond").click(function(){
	    var tip;
		
	    if($(this).is(":checked")){
	        if(!confirm("使用保证金后将不受区域保护，请慎用")){
	            return false;
		    }
		    
            <?php if($output['available_alliance_deposit'] + $output['available_alliance_bond'] >= $output['allamount']){?>
            tip = '使用品牌预存款支付<span style="color: #ED8600;padding:0 3px"><?php echo number_format($output['allamount'], 2);?></span>元';   
            <?php }else{?>
            tip = '<a style="color:red" href="index.php?act=brand_alliance&op=brand_pay&brand_id=<?php echo $output['order'][0]['brand_id'];?>">品牌预存款不足，请点击这里进行充值</a>';
            <?php }?> 	        
		}else{
            <?php if($output['available_alliance_deposit'] >= $output['allamount']){?>
            tip = '使用品牌预存款支付<span style="color: #ED8600;padding:0 3px"><?php echo number_format($output['allamount'], 2);?></span>元';   
            <?php }else{?>
            tip = '<a style="color:red" href="index.php?act=brand_alliance&op=brand_pay&brand_id=<?php echo $output['order'][0]['brand_id'];?>">品牌预存款不足，请点击这里进行充值</a>';
            <?php }?> 
	    }

	    $("#alliance_pay_tip").html(tip);
    });

    $('#check_balancepay').click(function(){
        if($('#check_balancepay').attr('checked') == 'checked'){
            $('#payment_cod').parents('li').hide(400);$('#payment_cod').attr('checked',false);
            //$('input[type="radio"]').attr('disabled',true);
            $('input[type="radio"]').attr('checked',false);
        }else{
            $('#payment_cod').parents('li').show(400);
            //$('input[type="radio"]').attr('disabled',false);
        }
    });
});
</script>
